package leecode

import (
	"fmt"
)

func IsValidSudoku(board [][]byte) bool {
	//每行只能出现一次
	for i := 0; i < 9; i++ {
		for j := 0; j < 9; j++ {
			if board[i][j] == '.' {
				continue
			}
			if !checkValue(i, j, board[i][j], board) {
				fmt.Println(i, j)
				return false
			}
		}
	}
	return true
}

func checkValue(row, column int, target byte, board [][]byte) bool {
	//当前行是否有效
	for i := 0; i < 9; i++ {
		if board[row][i] == target && i != column {
			return false
		}
	}
	//当前列是否有效
	for i := 0; i < 9; i++ {
		if board[i][column] == target && i != row {
			return false
		}
	}
	//3*3
	start := (row / 3) * 3
	end := (column / 3) * 3
	for i := start; i < start+3; i++ {
		for j := end; j < end+3; j++ {
			if board[i][j] == target && (i != row && j != column) {
				return false
			}
		}
	}
	return true
}
